package cn.yi.newsys.filter;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import cn.yi.newsys.pojo.User;

public class ManageFilter implements Filter{

	@Override
	public void destroy() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void doFilter(ServletRequest req, ServletResponse rsp,
			FilterChain chain) throws IOException, ServletException {
		User user = null ;
		HttpServletRequest request = (HttpServletRequest)req ;
		HttpSession session = request.getSession() ;
		HttpServletResponse response = (HttpServletResponse)rsp ;
		PrintWriter out = response.getWriter() ;
		
		response.setContentType("text/html;charset=UTF-8");
		Object obj = session.getAttribute("User") ;
		
		if(obj != null && obj instanceof User){
			user = (User)obj ;		
			if(user.getType() == 0){
				chain.doFilter(req, rsp) ;
			}else{
				out.print("<script>alert('请以管理员身份进入！');location.href='/News/index.jsp';</script>") ;
			}
		}else{
			out.print("<script>alert('请登录后再进入！');location.href='/News/index.jsp';</script>") ;
		}
		out.flush() ;
		out.close() ;
		
	}


	@Override
	public void init(FilterConfig arg0) throws ServletException {
		// TODO Auto-generated method stub
		
	}

}
